/*****************************************************************************

File:   otkFeatOperationsExample.java

Date          Build     Author        Version    Comments
18-Sep-12    P-20-14    dshah          $$1        Created
19-Oct-12    P-20-15    dshah          $$2        Updated
23-Oct-12    P-20-15    aphatak        $$3        Reduced message length in UIDisplayMessage
30-Oct-12    P-20-16    dshah          $$4        Updated												  
20-mar-15 P-30-05 gshmelev $$5  called GetCurrentSession
												  

****************************************************************************/


/**************************************************************************** 
 This example will work on the current model as input and do the following: 
	- Ask the user to select feature operation type ( 1-Suppress | 2-Resume | 3-Delete | 4-Reorder )
	- For Suppress/Delete - Ask the user to select upto 2 features to suppress/delete & do the operation
	- For Resume 
		- Ask the user to to enter the number of features to resume (max 2)
	    - Ask the user to enter the feat ids of the features to be resumed
		- Resume the features
    - Reorder Feature
	    - Ask the user to select upto 2 features to reorder
		- Ask the user to enter the Post-Reorder Feature Number 
*****************************************************************************/
package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFeatureInstructions.*;
import com.ptc.pfc.pfcSelect.*;
import com.ptc.pfc.pfcFeature.*;



class otkFeatOperationsExample extends DefaultUICommandActionListener
{
    
    
    
    public otkFeatOperationsExample()
    {
	
	
	
    }
    
    
    
    
    public void OnCommand()
    {
	
	addFeatOperationsExample();
	
    }
    
    
    
    
    public void addFeatOperationsExample()
    {
      	try
	    {
		FileWriter file = new FileWriter("ListFeatureOperations.txt");
		BufferedWriter fp_out_featops = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();
		WSolid myWSolid = (WSolid)(session.GetCurrentModel());
		
		session.UIDisplayMessage("feat_ops.txt", "USER Enter type of Feature Operation", null);
		
		Integer choice = session.UIReadIntMessage(1, 4);
		
		if (choice.intValue() == 1)
		    {
			/*Suppress Features*/
			otkSuppressFeatures (myWSolid);		
		    }
		
		if (choice.intValue() == 2)
		    {
			/*Resume Features*/
			otkResumeFeatures (myWSolid);
		    }
		
		if (choice.intValue() == 3)
		    {
			/*Delete Features*/
			otkDeleteFeatures (myWSolid);		
		    }
		
		if (choice.intValue() == 4)
		    {
			/*Reorder Features*/
			otkReorderFeatures (myWSolid);
		    }
		
		return;
	    }
	
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	
    }
    public void otkSuppressFeatures ( WSolid myWSolid )
    {
	
	SelectionOptions SelOptions;
	
	try
	    {
		FileWriter file = new FileWriter("SuppressFeature.txt");
		BufferedWriter fp_out_featops = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();
		
		fp_out_featops.write("Type of Feature Operation : SUPPRESS");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("Features Suppressed:");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("FEAT ID	       FEAT  TYPE");
		fp_out_featops.newLine();
		
		
		session.UIDisplayMessage("feat_ops.txt", "USER Select Features to Suppress (Max 2)", null);
		
		SelOptions = pfcSelect.SelectionOptions_Create("feature");
		
		SelOptions.SetMaxNumSels(new Integer(2));
		
		Selections FeatSelections = session.Select(SelOptions, null);
		
		intseq feat_ids = intseq.create();
		
		for (int i=0 ; i<FeatSelections.getarraysize() ; i++)
		    {
			ModelItem SelMItem = FeatSelections.get(i).GetSelItem();
			Feature feature = (Feature) SelMItem;
			
			fp_out_featops.write("  " + SelMItem.GetId() + "\t\t" + feature.GetFeatTypeName());
			fp_out_featops.newLine();
			
			feat_ids.append( SelMItem.GetId() );
		    }
		
		FeatSuppressOrDeleteOptions SuppOpts = FeatSuppressOrDeleteOptions.create();
		
		SuppOpts.append(FeatSuppressOrDeleteOption.FEAT_SUPP_OR_DEL_NO_OPTS );
		
		myWSolid.SuppressFeatures(feat_ids, SuppOpts, null);
		
		fp_out_featops.close();
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
    }
    public void otkResumeFeatures ( WSolid myWSolid )
    {
	SelectionOptions SelOptions;
	
	try
	    {
		FileWriter file = new FileWriter("ResumeFeature.txt");
		BufferedWriter fp_out_featops = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();
		
		fp_out_featops.write("Type of Feature Operation : RESUME");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("Features Resumed:");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("FEAT ID	       FEAT  TYPE");
		fp_out_featops.newLine();
		
		session.UIDisplayMessage("feat_ops.txt", "USER How many Features to Resume? (Max 2)", null);
		
		Integer num_feats = session.UIReadIntMessage(1, 2);
		
		intseq feat_ids = intseq.create();
		
		for (int i=0 ; i<num_feats.intValue() ; i++)
		    {
			session.UIDisplayMessage("feat_ops.txt", "USER Enter Feature-ID to Resume", null);
			
			Integer this_id = session.UIReadIntMessage(0, 32767);
			
			feat_ids.append(this_id.intValue());
			Feature feature = myWSolid.GetFeatureById(this_id.intValue());
			
			fp_out_featops.write("  " + this_id.intValue() + "\t\t" + feature.GetFeatTypeName());
			fp_out_featops.newLine();
		    }
		
		FeatResumeOptions ResOpts = FeatResumeOptions.create();
		
		ResOpts.append(FeatResumeOption.FEAT_RESUME_INCLUDE_PARENTS);
		
		myWSolid.ResumeFeatures(feat_ids, ResOpts, null);
		
		session.FlushCurrentWindow();
		fp_out_featops.close();
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
    }
    public void otkDeleteFeatures ( WSolid myWSolid )
    {
	SelectionOptions SelOptions;
	
	try
	    {
		FileWriter file = new FileWriter("DeleteFeature.txt");
		BufferedWriter fp_out_featops = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();
		
		fp_out_featops.write("Type of Feature Operation : DELETE");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("Features Resumed:");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("FEAT ID	       FEAT  TYPE");
		fp_out_featops.newLine();
		
		session.UIDisplayMessage("feat_ops.txt", "USER Select Features to Delete (Max 2)", null);
		
		SelOptions = pfcSelect.SelectionOptions_Create("feature");
		
		SelOptions.SetMaxNumSels(new Integer(2));
		
		Selections FeatSelections = session.Select(SelOptions, null);
		
		intseq feat_ids = intseq.create();
		
		for (int i=0 ; i<FeatSelections.getarraysize() ; i++)
		    {
			ModelItem SelMItem = FeatSelections.get(i).GetSelItem();
			Feature feature = (Feature) SelMItem;
			
			fp_out_featops.write("  " + SelMItem.GetId() + "\t\t" + feature.GetFeatTypeName());
			fp_out_featops.newLine();
			
			feat_ids.append( SelMItem.GetId() );
		    }
		
		FeatSuppressOrDeleteOptions DelOpts = FeatSuppressOrDeleteOptions.create();
		
		DelOpts.append(FeatSuppressOrDeleteOption.FEAT_SUPP_OR_DEL_NO_OPTS);
		
		myWSolid.DeleteFeatures(feat_ids, DelOpts, null);
		
		fp_out_featops.close();
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
    }
    public void otkReorderFeatures ( WSolid myWSolid )
    {
	SelectionOptions SelOptions;
	
	try
	    {
		FileWriter file = new FileWriter("ReorderFeature.txt");
		BufferedWriter fp_out_featops = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();
		
		fp_out_featops.write("Type of Feature Operation : REORDER");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("Before Reordering:");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("FEAT ID    FEAT NUM     FEAT TYPE");
		fp_out_featops.newLine();
		
		Features features1 = myWSolid.ListFeaturesByType(Boolean.TRUE, null);
		for(int j=0; j<features1.getarraysize(); j++)
		    {
			Feature feat1 = features1.get(j);
			
			fp_out_featops.write("  " + feat1.GetId() + "\t\t" + features1.get(j).GetNumber() + "\t" + features1.get(j).GetFeatTypeName());
			fp_out_featops.newLine();
		    }
		
		session.UIDisplayMessage("feat_ops.txt", "USER Select Features to Reorder (Max 2)", null);
		
		SelOptions = pfcSelect.SelectionOptions_Create("feature");
		
		SelOptions.SetMaxNumSels(new Integer(2));
		
		Selections FeatSelections = session.Select(SelOptions, null);
		
		intseq feat_ids = intseq.create();
		
		for (int i=0 ; i<FeatSelections.getarraysize() ; i++)
		    {
			ModelItem SelMItem = FeatSelections.get(i).GetSelItem();
			
			feat_ids.append( SelMItem.GetId() );
		    }
		
		session.UIDisplayMessage("feat_ops.txt", "USER Enter New Feature Number Post-Reorder (of 1st feature in Reorder set):", null);
		
		Integer new_feat_num = session.UIReadIntMessage(0, 32767);
		
		myWSolid.ReorderFeatures(feat_ids, new_feat_num.intValue(), null);
		
		fp_out_featops.newLine();
		fp_out_featops.write("After Reordering:");
		fp_out_featops.newLine();
		fp_out_featops.newLine();
		fp_out_featops.write("FEAT ID    FEAT NUM     FEAT TYPE");
		fp_out_featops.newLine();
		
		Features features2 = myWSolid.ListFeaturesByType(Boolean.TRUE, null);
		for(int j=0; j<features2.getarraysize(); j++)
		    {
			Feature feat2 = features2.get(j);
			
			fp_out_featops.write("  " + feat2.GetId() + "\t\t" + features2.get(j).GetNumber() + "\t" + features2.get(j).GetFeatTypeName());
			fp_out_featops.newLine();
		    }
		
		fp_out_featops.close();
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
    }
    
};
